package com.dingding.sync.router;

import com.dingding.sync.ding.service.GoodsService;
import com.dingding.sync.repository.entity.Goods;
import com.dingding.sync.repository.request.GoodsRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zhangyanan
 * @Date: 2020-05-03 22:07
 * @Description:
 */
@RestController
@CrossOrigin(origins = {"http://localhost:3000","http://localhost:3001"},allowCredentials = "true",allowedHeaders = {"*"},
       maxAge = 3600L, methods={RequestMethod.GET,RequestMethod.PUT,RequestMethod.POST,RequestMethod.DELETE,RequestMethod.HEAD,RequestMethod.OPTIONS})
@Slf4j
public class GoodsRouter {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/goods/list")
    public List<Goods> getGoodsList(@RequestParam(required = false) String title) {
        log.info("title:{}",title);
        title = title == null?"":title;
        return goodsService.getGoodsList(title);
    }

    /**
     * 新增
     * @return
     */
    @PutMapping("/goods/add")
    public void createGoods(@RequestBody GoodsRequest request) {
        log.info("[createGoods] the parameters of createGoods request is {}", request.toString());
        goodsService.addGoods(request);
    }


    /**
     * 更新
     * @return
     */
    @PostMapping("/goods/update")
    public void updateGoods(@RequestBody GoodsRequest request) {
        log.info("[updateGoods] the parameters of updateGoods request is {}", request.toString());
        goodsService.updateGoods(request);

    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/goods/delete")
    public void updateGoods( @RequestParam(required = true) String id) {

        log.info("[updateGoods] the parameters of delete id is {}", id);
        goodsService.deleteGoods(Integer.valueOf(id));

    }
}
